package com.tqz.zookeeper.queue.api;

import com.tqz.zookeeper.common.ZookeeperUtil;
import com.tqz.zookeeper.queue.springboot.ZKQueueUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.queue.*;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * 分布式队列生产者.
 *
 * @author <a href="https://github.com/tian-qingzhao">tianqingzhao</a>
 * @since 2025/9/5 16:45
 */
public class CuratorDistributedQueueProducerTest {

    private static final String QUEUE_ROOT = "/curator_distributed_queue";

    public static void main(String[] args) throws Exception {
        CuratorFramework client = CuratorFrameworkFactory.newClient(ZookeeperUtil.ZK_URL,
                new ExponentialBackoffRetry(1000, 3));
        client.start();

        // 创建分布式队列
        DistributedQueue<String> queue = QueueBuilder.builder(
                        client,
                        ZKQueueUtils.queueConsumer(),
                        ZKQueueUtils.createQueueSerializer(),
                        QUEUE_ROOT)
                .buildQueue();
        queue.start();

        // 生产消息
        for (int i = 0; i < 5; i++) {
            String message = "Task-" + i;
            System.out.println("生产消息: " + message);
            queue.put(message);
            Thread.sleep(1000);
        }

        Thread.sleep(10000);
        queue.close();
        client.close();
    }
}
